package com.ruoyi.web.controller.stock;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.system.domain.vo.StockDayVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StockDay;
import com.ruoyi.system.service.IStockDayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 每日记录Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/stock/day")
public class StockDayController extends BaseController
{
    @Autowired
    private IStockDayService stockDayService;

    /**
     * 查询每日记录列表
     */
    @PreAuthorize("@ss.hasPermi('stock:day:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockDay stockDay)
    {
        startPage();
        List<StockDay> list = stockDayService.selectStockDayList(stockDay);
        return getDataTable(list);
    }

    /**
     * 导出每日记录列表
     */
    @PreAuthorize("@ss.hasPermi('stock:day:export')")
    @Log(title = "每日记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockDay stockDay)
    {
        List<StockDay> list = stockDayService.selectStockDayList(stockDay);
        ExcelUtil<StockDay> util = new ExcelUtil<StockDay>(StockDay.class);
        util.exportExcel(response, list, "每日记录数据");
    }

    /**
     * 每日板块数据导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('stock:day:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<StockDayVO> util = new ExcelUtil<StockDayVO>(StockDayVO.class);
        List<StockDayVO> plateList = util.importExcel(file.getInputStream());
        String date = "20240105";
        String message = stockDayService.importUser(plateList, updateSupport, date);
        return AjaxResult.success(message);
    }

    /**
     * 模板下载
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<StockDay> util = new ExcelUtil<StockDay>(StockDay.class);
        util.importTemplateExcel(response, "板块数据");
    }


    /**
     * 获取每日记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:day:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(stockDayService.selectStockDayByRecordId(recordId));
    }

    /**
     * 获取每日记录详细信息
     */
    @GetMapping("/readFile")
    public AjaxResult readFile(String path)
    {
        return success(stockDayService.readFile(path));
    }

    /**
     * 筛选数据
     */
    @GetMapping("/screen")
    public AjaxResult screen(String path)
    {
        return success(stockDayService.screen(path));
    }

    @GetMapping("/callThx")
    public AjaxResult callThx(String path)
    {
        //绝杀：竞价涨幅*竞价实际换手率的乘积从高到低前十名 非st的股票
        //首板翘尾：昨日连续涨停天数=1 今日09点25的分时最低价>09点24的分时最低价 剔除st 剔除新股
        //一进二：昨日连续涨停天数=1 今日竞价成交量/昨日一分钟最大成交量大于50% 非st 非北交所 非新股
        //二板翘尾：昨日连续涨停天数>=2 今日09点25的分时最低价>09点24的分时最低价 剔除st 剔除新股
        //成交金额：昨日涨停 今日竞价成交金额/昨日成交总金额大于10% 非st 非北交所 非新股
        //二进三：昨日连续涨停天数=2 今日竞价成交量/昨日成交量大于5.3% 非st 非北交所 非新股
        //三进四：昨日连续涨停天数=3 今日竞价成交量/昨日成交量大于10.3% 非st 非北交所 非新股
        //大屁股战法：竞价价格大于昨日ma10价格且竞价价格小于昨日ma10价格*1.05或竞价价格大于昨日ma5价格且竞价价格小于昨日ma5价格*1.05或竞价价格大于昨日ma3价格且竞价价格小于昨日ma3价格*1.03 昨日成交金额大于25亿 昨日收盘价大于21日线 昨日非涨停 今日竞价金额大于2500万 竞价异动类型是竞价抢筹或涨停试盘
        String map= HttpUtil.get("http://apigate.10jqka.com.cn/d/platform/dynamicplate/stocks/self/v2/select?query=%E6%98%A8%E6%97%A5%E8%BF%9E%E7%BB%AD%E6%B6%A8%E5%81%9C%E5%A4%A9%E6%95%B0=1,%E4%BB%8A%E6%97%A5%E7%AB%9E%E4%BB%B7%E6%88%90%E4%BA%A4%E9%87%8F/%E6%98%A8%E6%97%A5%E4%B8%80%E5%88%86%E9%92%9F%E6%9C%80%E5%A4%A7%E6%88%90%E4%BA%A4%E9%87%8F%E5%A4%A7%E4%BA%8E50%25,%E9%9D%9Est,%E9%9D%9E%E5%8C%97%E4%BA%A4%E6%89%80,%E9%9D%9E%E6%96%B0%E8%82%A1&num=1000");
        //这个可以获取同花顺的开盘赛选代码
        JSONObject jsonObject = JSONUtil.parseObj(map);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray codes = data.getJSONArray("codes");
        Map o = (Map) codes.get(0);
        System.out.println(o.get("code"));
        return null;
    }


    /**
     * 新增每日记录
     */
    @PreAuthorize("@ss.hasPermi('stock:day:add')")
    @Log(title = "每日记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockDay stockDay)
    {
        return toAjax(stockDayService.insertStockDay(stockDay));
    }

    /**
     * 修改每日记录
     */
    @PreAuthorize("@ss.hasPermi('stock:day:edit')")
    @Log(title = "每日记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockDay stockDay)
    {
        return toAjax(stockDayService.updateStockDay(stockDay));
    }

    /**
     * 删除每日记录
     */
    @PreAuthorize("@ss.hasPermi('stock:day:remove')")
    @Log(title = "每日记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(stockDayService.deleteStockDayByRecordIds(recordIds));
    }
}
